package com.ruoyi.web.zangchat.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.text.Convert;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.zangchat.domain.ChatLabel;
import com.ruoyi.web.zangchat.service.IChatLabelService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 标签Controller
 * 
 * @author ruoyi
 * @date 2020-07-24
 */
@Controller
@RequestMapping("/zangchat/chatlabel")
public class ChatLabelController extends BaseController
{
    private String prefix = "zangchat/chatlabel";

    @Autowired
    private IChatLabelService chatLabelService;

    @RequiresPermissions("zangchat:chatlabel:view")
    @GetMapping()
    public String chatlabel()
    {
        return prefix + "/chatlabel";
    }

    /**
     * 查询标签列表
     */
    @RequiresPermissions("zangchat:chatlabel:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ChatLabel chatLabel)
    {
        startPage();
        return getDataTable(chatLabelService.list(new QueryWrapper<>()));
    }

    /**
     * 导出标签列表
     */
    @RequiresPermissions("zangchat:chatlabel:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ChatLabel chatLabel)
    {
        List<ChatLabel> list = chatLabelService.list(new QueryWrapper<>());
        ExcelUtil<ChatLabel> util = new ExcelUtil<ChatLabel>(ChatLabel.class);
        return util.exportExcel(list, "chatlabel");
    }

    /**
     * 新增标签
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存标签
     */
    @RequiresPermissions("zangchat:chatlabel:add")
    @Log(title = "标签", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ChatLabel chatLabel)
    {
//        return toAjax(chatLabelService.save(chatLabel));
        return chatLabelService.saveChatLabel(chatLabel);
    }

    /**
     * 修改标签
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ChatLabel chatLabel = chatLabelService.getById(id);
        mmap.put("chatLabel", chatLabel);
        return prefix + "/edit";
    }

    /**
     * 修改保存标签
     */
    @RequiresPermissions("zangchat:chatlabel:edit")
    @Log(title = "标签", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ChatLabel chatLabel)
    {
//        return toAjax(chatLabelService.updateById(chatLabel));
        return chatLabelService.updateChatLabelById(chatLabel);
    }

    /**
     * 删除标签
     */
    @RequiresPermissions("zangchat:chatlabel:remove")
    @Log(title = "标签", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(chatLabelService.removeByIds(Arrays.asList(Convert.toStrArray(ids))));
    }
}
